var webSocket;

function connect() {
  webSocket = new WebSocket('ws://localhost:8180/prf-game/websocket-game'); 

  webSocket.onerror = function(event) {
    onError(event)
  };

  webSocket.onopen = function(event) {
    onOpen(event)
  };

  webSocket.onmessage = function(event) {
    onMessage(event)
  };
}

function onMessage(event) {
  document.getElementById('messages').innerHTML 
    += '<br />Received message: ' + event.data;
}

function onOpen(event) {
  document.getElementById('messages').innerHTML 
    = 'Connection established';
}

function onError(event) {
  alert(event.data);
}

function send() {
  var txt = document.getElementById('inputmessage').value;
  webSocket.send(txt);
  document.getElementById('messages').innerHTML 
  += '<br />Sended message: ' + txt;
  return false;
}

function disconnect() {
    webSocket.close();
    document.getElementById('messages').innerHTML 
    += '<br />' + 'Connection closed';
    return false;
}

//window.addEventListener("load", connect, false);
